/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-dealer
*
* @File name : GetCarController.java
*
* @Author : Administrator
*
* @Date : 2017年12月5日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年12月5日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/
	
package com.yonyou.gmmc.service.dealer.controller;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.Response;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.yonyou.gmmc.service.dealer.constant.DealerConstant;
import com.yonyou.gmmc.service.dealer.model.TtEtco;
import com.yonyou.gmmc.service.dealer.service.GetCarService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
*
* @author Administrator
* 企业号上门取送车API
* @date 2017年12月5日
*/
@RestController
@RequestMapping(value = DealerConstant.BASE_PATH + "/" + DealerConstant.VERSION)
@Api("企业号上门取送车API")
public class GetCarController {

    private Logger logger = Logger.getLogger(GetCarController.class);
    
    @Autowired
    private GetCarService getCarService;
    
    /**
     * 查询取送车列表
     * @author QuanKK
     * @date 2018年1月23日 下午3:07:39
     * @param pageNum
     * @param pageSize
     * @param flag
     * @param userPhone
     * @param licenseNo
     * @param submitDate
     * @param orderType
     * @param orderStatus
     * @param phone
     * @return
     */
    @SuppressWarnings("static-access")
	@RequestMapping(value = "/queryEtcoList", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "获取该员工所在店的取送车列表", notes = "获取该员工所在店的取送车列表")
	public Response<PageInfo<TtEtco>> queryEtcoList(
			@RequestParam(required = false, defaultValue = "10") Integer pageNum
			, @RequestParam(required = false, defaultValue = "10") String pageSize
			, @RequestParam(required = false, defaultValue = "3") String flag       //1.今日取送;2.未完成;3.全部
			, @RequestParam(required = false, defaultValue = "") String phoneOrLicense 	// 客户电话// 车牌号
			, @RequestParam(required = false, defaultValue = "") String submitDate  //提交时间：1.今天;2.一周内;3.开始日期-截止日期
			, @RequestParam(required = false, defaultValue = "") Integer orderType  //订单类型：0.全部; 10471002.取车;10471003.送车;10471001.取送车;
			, @RequestParam(required = false, defaultValue = "") Integer orderStatus//取送车订单状态  0.全部; 10191001:未下单，10191002:下单成功,10191003:派单成功，10191004:车辆送达,10191005:下单取消
			, @RequestParam("phone") String phone        							// 售后顾问电话
	) {
		logger.info("查询取送车列表。。。用户》》》》》" + phone);
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
			paramMap.put("pageNum", pageNum);
			paramMap.put("pageSize", pageSize);
			paramMap.put("flag", flag);
			paramMap.put("phoneOrLicense", phoneOrLicense);
			if(!CheckUtil.NullOrEmpty(submitDate)){
                String submitStartDate = "";
                String submitEndDate = "";
                if(submitDate.equals("1")){
                    Date date=new Date();
                    submitStartDate = sdf.format(date)+" 00:00:00";
                    submitEndDate = sdf.format(date)+" 23:59:59";
                }else if(submitDate.equals("2")){
                    Date date=new Date();
                    Calendar calendar = new GregorianCalendar();  
                    calendar.setTime(date);  
                    calendar.add(calendar.DATE,-7);
                    date=calendar.getTime(); 
                    submitStartDate = sdf.format(date)+" 00:00:00";
                    submitEndDate = sdf.format(new Date())+" 23:59:59";
                }else{
                    String a[] = submitDate.split("-");
                    submitStartDate = a[0]+" 00:00:00";
                    submitEndDate = a[1]+" 23:59:59";
                }
                paramMap.put("submitStartDate", submitStartDate);
                paramMap.put("submitEndDate", submitEndDate);
            }
			paramMap.put("orderType", orderType);
			paramMap.put("orderStatus", orderStatus);
			paramMap.put("phone", phone);
			PageInfo<TtEtco> result = getCarService.queryEtcoList(paramMap);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		}
	}
    
	/**
	 * 取送车详情
	 * @author QuanKK
	 * @date 2018年1月24日 上午10:59:24
	 * @param etcoId
	 * @param phone
	 * @return
	 */
	@RequestMapping(value = "/queryEtcoDetail", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "查看取送（车）订单详情", notes = "查看取送（车）订单详情")
	public Response<TtEtco> queryEtcoDetail(
			@RequestParam(required=true,defaultValue="") Integer etcoId
			, @RequestParam("phone") String phone 	// 售后顾问电话
			) {
		logger.info("查询取送车详情GetCarController.queryEtcoDetail==>start...用户："+phone);
		try {
			TtEtco ttEtco = getCarService.queryEtcoDetail(etcoId,phone);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, ttEtco, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.queryEtcoDetail==>end...");
		}
	}
    
	/**
	 * 取消取送车
	 * @author QuanKK
	 * @date 2018年1月24日 上午10:59:08
	 * @param etcoId
	 * @param phone
	 * @return
	 */
	@RequestMapping(value = "/cancelCar", method = RequestMethod.POST)
	@ApiOperation(value = "取消取送车", notes = "取消取送车")
	public Response<Map<String, Object>> cancelCar(
			@RequestParam(required=false,defaultValue="") String etcoId
			, @RequestParam("phone") String phone 	// 售后顾问电话
			) {
		logger.info("取消取送车GetCarController.cancelCar==>start...用户："+phone);
		try {
			Map<String, Object> map = getCarService.cancelGetSendCar(etcoId,phone);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.cancelGetSendCar==>end...");
		}
	}
	
	/**
	 * 查看进度
	 * @author QuanKK
	 * @date 2018年1月24日 上午11:46:20
	 * @param etcoId
	 * @param phone
	 * @return
	 */
	@RequestMapping(value = "/queryQscProgress", method = RequestMethod.POST)
	@ApiOperation(value = "查看进度", notes = "查看进度")
	public Response<Map<String, Object>> queryQscProgress(
			@RequestParam(required=true,defaultValue="") String etcoId
			, @RequestParam("phone") String phone 	// 售后顾问电话
			) {
		logger.info("查看进度GetCarController.queryQscProgress==>start...顾问电话："+phone);
		try {
			Map<String, Object> map = getCarService.queryQscProgress(etcoId,phone);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.queryQscProgress==>end...");
		}
	}

	/**
	 * 查看轨迹信息
	 * @author QuanKK
	 * @date 2018年1月24日 上午11:52:20
	 * @param etcoId
	 * @param phone
	 * @return
	 */
	@RequestMapping(value = "/queryQscTrack", method = RequestMethod.POST)
	@ApiOperation(value = "查看轨迹信息", notes = "查看轨迹信息")
	public Response<Map<String, Object>> queryQscTrack(
			@RequestParam(required=true,defaultValue="") String etcoId
			, @RequestParam("phone") String phone 	// 售后顾问电话
			) {
		logger.info("查看轨迹信息GetCarController.queryQscTrack==>start...顾问电话："+phone);
		try {
			Map<String, Object> map = getCarService.queryQscTrack(etcoId,phone);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.queryQscTrack==>end...");
		}
	}

	/**
	 * 查看司机信息
	 * @author QuanKK
	 * @date 2018年1月24日 上午11:58:06
	 * @param etcoId
	 * @param phone
	 * @return
	 */
	@RequestMapping(value = "/queryDriverinfo", method = RequestMethod.POST)
	@ApiOperation(value = "查看司机信息", notes = "查看司机信息")
	public Response<Map<String, Object>> queryDriverinfo(
			@RequestParam(required=true,defaultValue="") String etcoId
			, @RequestParam("phone") String phone 	// 售后顾问电话
			) {
		logger.info("查看司机信息GetCarController.queryDriverinfo==>start...顾问电话："+phone);
		try {
			Map<String, Object> map = getCarService.queryDriverinfo(etcoId,phone);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.queryDriverinfo==>end...");
		}
	}
	
	
    
    
    
}
